#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <string.h>

int minAreaRect(int** points, int pointsSize, int* pointsColSize) {
	if(pointsSize < 4){
		*pointsColSize = 0;
		return 0;
	}
	int (*wide)[3] = (int(*)[3])malloc(250 * 3 * sizeof(int));
	int (*high)[3] = (int(*)[3])malloc(250 * 3 * sizeof(int));
	for(int i = 0; i < 250; i++){
		memset(wide[i], -1, 3 * sizeof(int));
		memset(high[i], -1, 3 * sizeof(int));
	}
	bool *is_w = (bool*)malloc(250 * sizeof(bool));
	bool *is_h = (bool*)malloc(250 * sizeof(bool));
	int w_num = 0, h_num = 0;
	for(int i = 0; i < pointsSize; i++){
		bool is_y_exist = false;
		bool is_x_exist = false;
		int j = 0;
		while(j < w_num || j < h_num){
			if(!is_y_exist && j < w_num && wide[j][0] == points[i][1]){
				is_y_exist = true;
				is_w[j] = true;
				if(wide[j][2] == -1){
					if(wide[j][1] < points[i][0]){
						wide[j][2] = points[i][0];
					} else {
						wide[j][2] = wide[j][1];
						wide[j][1] = points[i][0];	
					}
				} else {
					wide[j][1] = (wide[j][1] < points[i][0]) ? wide[j][1] : points[i][0];
					wide[j][2] = (wide[j][2] > points[i][0]) ? wide[j][2] : points[i][0];
				}
				if(is_x_exist){
					break;
				}
			}
			if(!is_x_exist && j < h_num && high[j][0] == points[i][0]){
				is_x_exist = true;
				is_h[j] = true;
				if(high[j][2] == -1){
					if(high[j][1] < points[i][1]){
						high[j][2] = points[i][1];
					} else {
						high[j][2] = high[j][1];
						high[j][1] = points[i][1];	
					}
				} else {
					high[j][1] = (high[j][1] < points[i][1]) ? high[j][1] : points[i][1];
					high[j][2] = (high[j][2] > points[i][1]) ? high[j][2] : points[i][1];
				}
				if(is_y_exist){
					break;
				}
			}
			j++;
		}
		if(!is_y_exist){
			wide[w_num][0] = points[i][1];
			wide[w_num][1] = points[i][0];
			is_w[w_num] = false;
			w_num++;
		}
		if(!is_x_exist){
			high[h_num][0] = points[i][0];
			high[h_num][1] = points[i][1];
			is_h[h_num] = false;
			h_num++;
		}
	}
	*pointsColSize = 2147483647;
	for(int i = 0; i < w_num; i++){
		if(is_w[i]){
			for(int j = 0; j < h_num; j++){
				if(is_h[j] && wide[i][2] >= high[j][0] && wide[i][1] <= high[j][0] && wide[i][0] >= high[j][1] && wide[i][0] <= high[j][2]){
					for(int k = i + 1; k < w_num; k++){
						if(is_w[k] && wide[k][0] >= high[j][1] && wide[k][0] <= high[j][2]){
							int h = (wide[i][0] > wide[k][0]) ? wide[i][0] - wide[k][0] : wide[k][0] - wide[i][0];
							int wj = high[j][0], w;
							if(wj == wide[i][1] || wj == wide[k][1]){
								w = ((wide[i][2] <= wide[k][2]) ? wide[i][2] : wide[k][2]) - wj;
							} else if(wj == wide[i][2] || wj == wide[k][2]){
								w = wj - ((wide[i][1] <= wide[k][1]) ? wide[i][1] : wide[k][1]);
							} else {
								int wi1 = wj - wide[i][1];
								int wi2 = wide[i][2] - wj;
								int wk1 = wj - wide[k][1];
								int wk2 = wide[k][2] - wj;
								wi1 = (wi1 <= wi2) ? wi1 : wi2;
								wk1 = (wk1 <= wk2) ? wk1 : wk2;
								w = (wi1 <= wk1) ? wi1 : wk1;
							}
							*pointsColSize = (*pointsColSize > (h * w)) ? (h * w) : *pointsColSize;
						}
					}
				}
			}
		}
	}
	if(*pointsColSize == 2147483647){
		return 0;
	}
	return *pointsColSize;
}

int main (int argc, char *argv[]) {
	int points[][2] = {{1,1},{1,3},{3,1},{3,3},{2,2}};
	int *p[5];
	for(int i = 0; i < 5; i++){
		p[i] = points[i];
	}
	int m_a_r = 0;
	printf("%d\n", minAreaRect(p, 5, &m_a_r));
	return 0;
}
